package com.zhai.cloud.common.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Created by 翟凤玺
 * @package com.zhai.boot.util
 * @project-name zhai-cloud
 * @description BigDecimal工具类
 * @create-datetime 2019-2-18 14:56
 */
public class BigDecimalUtil {
    /**
     * 比较结果大于
     */
    public static final Integer COMPARE_GREATER_THAN = 1;
    /**
     * 比较结果等于
     */
    public static final Integer COMPARE_EQUAL = 0;
    /**
     * 比较结果小于
     */
    public static final Integer COMPARE_LESS_THAN = -1;

    /**
     * 初始化小数
     */
    public static BigDecimal initDecimal(String decimal) {
        return new BigDecimal(decimal);
    }

    /**
     * 取余
     */
    public static BigDecimal[] remainder(BigDecimal number, BigDecimal passiveNumber) {
        return number.divideAndRemainder(passiveNumber);
    }

    /**
     * 获取取余结果的商
     */
    public static BigDecimal getRemainderGuotient(BigDecimal[] remainderResult) {
        return remainderResult[0];
    }

    /**
     * 获取取余结果的余数
     */
    public static BigDecimal getRemainderNumber(BigDecimal[] remainderResult) {
        return remainderResult[1];
    }

    /**
     * 转为Intger类型
     */
    public static Integer toInteger(BigDecimal number) {
        return number.intValue();
    }

    /**
     * 比较
     */
    public static Integer compareTo(BigDecimal number, BigDecimal passiveNumber) {
        return number.compareTo(passiveNumber);
    }

    /**
     * number 是否大于 passiveNumber
     */
    public static Boolean greaterThan(BigDecimal number, BigDecimal passiveNumber) {
        return COMPARE_GREATER_THAN.equals(BigDecimalUtil.compareTo(number, passiveNumber));
    }

    /**
     * number 是否等于 passiveNumber
     */
    public static Boolean equal(BigDecimal number, BigDecimal passiveNumber) {
        return COMPARE_EQUAL.equals(BigDecimalUtil.compareTo(number, passiveNumber));
    }

    /**
     * number 是否小于 passiveNumber
     */
    public static Boolean lessThan(BigDecimal number, BigDecimal passiveNumber) {
        return COMPARE_LESS_THAN.equals(BigDecimalUtil.compareTo(number, passiveNumber));
    }

    /**
     * 拆分
     * 例如：
     * 入参：number = 11.1, passiveNumber = 5
     * 出参：[5,5,1.1]
     */
    public static List<BigDecimal> split(BigDecimal number, BigDecimal passiveNumber) {
        BigDecimal[] remainder = BigDecimalUtil.remainder(number, passiveNumber);
        BigDecimal guotient = BigDecimalUtil.getRemainderGuotient(remainder);
        BigDecimal remainderNumber = BigDecimalUtil.getRemainderNumber(remainder);
        Integer time = BigDecimalUtil.toInteger(guotient);
        List<BigDecimal> result = new ArrayList<>();
        for (int i = 0; i < time; i++) {
            result.add(passiveNumber);
        }
        if (BigDecimalUtil.greaterThan(remainderNumber, BigDecimal.ZERO)) {
            result.add(remainderNumber);
        }
        return result;
    }

    public static void main(String[] args) {
        BigDecimal bigDecimal = BigDecimalUtil.initDecimal("10.001");
        BigDecimal a = new BigDecimal(5);
        BigDecimal[] remainder = BigDecimalUtil.remainder(bigDecimal, a);
        BigDecimal guotient = BigDecimalUtil.getRemainderGuotient(remainder);
        BigDecimal remainderNumber = BigDecimalUtil.getRemainderNumber(remainder);
        Integer time = BigDecimalUtil.toInteger(guotient);
        System.out.println("商：" + guotient);
        System.out.println("余数：" + remainderNumber);
        System.out.println("转为interger：" + time);
        System.out.println("比较：" + BigDecimalUtil.compareTo(remainderNumber, new BigDecimal("0.000")));
        List<BigDecimal> split = BigDecimalUtil.split(bigDecimal, a);
        System.out.println(JSONUtil.toStr(split));
    }
}
